// 处理数据为空
import { message } from "antd";

// 如果传入null则返回'-',否则返回它本身
const hb = (obj) => {
  if (obj == null || obj === "") return "-";
  return obj;
};

// 如果在数据不为空时对数据进行处理
// 返回处理后的数据
const hb2 = (obj, callback = (d) => d) => {
  if (obj == null || obj === "") return "-";
  return callback(obj);
};

const promptResult = async (
  res,
  bindCallback = () => {},
  closeCallback = () => {},
  successMessage = "成功了",
  failMessage = "失败了"
) => {
  if (res.status >= 0) {
    await bindCallback();
    message.success(res.message || successMessage);
  } else {
    message.error(res.message || failMessage);
  }
  await closeCallback(false);
};

const promptResult2 = async (
  promise,
  setLoading = () => {},
  bind = () => {},
  successMsg = "成功了",
  failMsg = "失败了"
) => {
  setLoading(true);
  const res = await promise;
  if (res.status >= 0) {
    message.success(res.message || successMsg);
    bind(); // 不必等待
  } else {
    message.error(res.message || failMsg);
  }
  setLoading(false);
};

const getImagePath = (id, imageName) => {
  return (
    import.meta.env.VITE_BASE_URL + "/Images/Supplier/" + id + "/" + imageName
  );
};
const getImagePath2 = (id, imageName) => {
  return (
    import.meta.env.VITE_BASE_URL + "/Images/Goods/" + id + "/" + imageName
  );
};

export { hb, hb2, promptResult, promptResult2, getImagePath, getImagePath2 };
